package com.kayak.weixin.util;

import java.util.Arrays;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 接入认证工具类
 * @author myt
 *
 */
public class SignUtil {

	public static boolean checkSignature(String token,String signature,
			String timestamp, String nonce) {

		String temp = getSignature(token,timestamp,nonce);  
		if(signature.equals(temp)){
			return true;
		}else{
			return false;
		}
	}
	
	public static String getSignature(String token,
			String timestamp, String nonce) {

		//到字符串数组中
		String[] arr = new String[] { token, timestamp, nonce };
		// 将token、timestamp、nonce三个参数进行字典序排序
		Arrays.sort(arr);
		StringBuilder content = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		String temp =  DigestUtils.shaHex(content.toString());  
		
		return temp;
	}

}
